using System;
using System.Collections;

namespace MFWebSockets.Core.Utilities.Collections.NameValue
{
    public class NameValueCollection : ICollection
    {
         ArrayList _pairs = new ArrayList();
         object _syncRoot = new object();

        public string this[int index]
        {
            get { return Get(index); }
        }

        public string this[string name]
        {
            get { return Get(name); }
            set { Set(name, value); }
        }

        public string[] AllKeys
        {
            get { return (string[])GetKeys().ToArray(typeof(string)); }
        }

        public ArrayList Keys
        {
            get { return GetKeys(); }
        }

        public void Add(string name, string value)
        {
            AddWithoutValidate(name, value);
        }

        protected void AddWithoutValidate(string name, string value)
        {
            _pairs.Add(new NameValuePair(name, value));
        }

        public void Clear()
        {
            _pairs.Clear();
        }

        public string Get(int index)
        {
            return ((NameValuePair)_pairs[index]).Value;
        }

        public string Get(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                NameValuePair nvp = (NameValuePair)_pairs[i];
                if (nvp.Name == name)
                {
                    return nvp.Value;
                }
            }
            return null;
        }

        public string GetKey(int index)
        {
            return ((NameValuePair)_pairs[index]).Name;
        }

        public void Remove(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                NameValuePair nvp = (NameValuePair)_pairs[i];
                if (nvp.Name == name)
                {
                    _pairs.RemoveAt(i);
                    break;
                }
            }
        }

        public void Set(string name, string value)
        {
            for (int i = 0; i < Count; i++)
            {
                NameValuePair nvp = (NameValuePair)_pairs[i];
                if (nvp.Name == name)
                {
                    nvp.Value = value;
                    return;
                }
            }
            Add(name, value);
        }

         ArrayList GetKeys()
        {
            ArrayList list = new ArrayList();
            foreach (NameValuePair nvp in _pairs)
            {
                list.Add(nvp.Name);
            }
            return list;
        }

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            _pairs.CopyTo(array, index);
        }

        public int Count
        {
            get { return _pairs.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return _syncRoot; }
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return new NameValueEnumerator(this);
        }

        #endregion
    }
}
